package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.edu.cn/ 中国教育
 *  abc
 */
public class EduNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.edu.cn/.*/[0-9]{8}/t[0-9]{8}_[0-9]*.shtml",
				"http://www.edu.cn/.*/.*/.*/t[0-9]{8}_[0-9]*.shtml",
				
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);
		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select(".biaoti04").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, " - ");
			title=StringUtils.substringBefore(title, "—");
			title=StringUtils.substringBefore(title, "--");
		}
		date=DateUtils.matchDate(doc.select(".STYLE17").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("p.js_03_lp").text());
			if(date==null){
				date=JSoupUtils.matchDate(doc,"发布时间");
				if(date==null){
					date=DateUtils.matchDate(doc.select("p.page_time").text());
					if(date==null){
						date=DateUtils.matchDate(url);
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("#tabletext"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("#mcontent p"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("#table1"));
			}
		}
		
		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
			
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		EduNewsAnalyse a=new EduNewsAnalyse();
		String url="http://www.edu.cn/ke_yan_yu_fa_zhan/gao_xiao_cheng_guo/cheng_guo_zhan_shi/201509/t20150915_1315975.shtml";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println("url="+fromatUrl);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
